package com.scqkxx.gamebox.activity.activities.utils;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.util.Log;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.FutureTarget;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.scqkxx.gamebox.R;
import com.scqkxx.gamebox.activity.myself.GlideCircleImage;
import com.scqkxx.gamebox.utils.GlideRoundedCornersTransform;
import com.scwang.smartrefresh.layout.util.DensityUtil;

import java.io.File;

/**
 * name: ts
 * 描述：Glide工具类（glide 4.x）
 * 功能包括加载图片，圆形图片，
 */
public class GlideUtils {

    public static final int placeholderSoWhite = R.color.colorAccent;
    public static final int errorSoWhite = R.mipmap.moren;

    /*
     *加载图片(默认)
     */
    public static void loadImage(Context context, String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholderSoWhite) //占位图
                .error(errorSoWhite)       //错误图
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL); //磁盘缓存
        Glide.with(context).load(url).apply(options).into(imageView);

    }

    /**
     * 指定图片大小;使用override()方法指定了一个图片的尺寸。
     */
    public static void loadImageSize(Context context , String url, ImageView imageView, int width, int height) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .placeholder(placeholderSoWhite) //占位图
                .error(errorSoWhite)       //错误图
                .override(width , height)
                // .priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context).load(url).apply(options).into(imageView);
    }


    /**
     * 禁用内存缓存功能
     */
    public static void loadImageSizekipMemoryCache( Context context , String url , ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholderSoWhite) //占位图
                .error(errorSoWhite)       //错误图S
                .skipMemoryCache(true)//禁用掉Glide的内存缓存功能
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context).load(url).apply(options).into(imageView);

    }


    /**
     * 加载圆形图片
     */
    public static void loadCircleImage(Context context, String url, ImageView imageView) {
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .circleCrop()//设置圆形
                .placeholder(placeholderSoWhite)
                .error(errorSoWhite)
                //.priority(Priority.HIGH)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context).load(url).apply(options).into(imageView);
    }

    /**
     * 预先加载图片
     */
    public static void preloadImage( Context context, String url ) {
        Glide.with(context)
                .load(url)
                .preload();
    }


    /**
     * Glide.with(this).asGif()    //强制指定加载动态图片
     */
    private void loadGif( Context context, String url, ImageView imageView ) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeholderSoWhite)
                .error(errorSoWhite);
        Glide.with(context)
                .load(url)
                .apply(options)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed( @Nullable GlideException e , Object model , Target<Drawable> target , boolean isFirstResource ) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady( Drawable resource , Object model , Target<Drawable> target , DataSource dataSource , boolean isFirstResource ) {
                        return false;
                    }
                })
                .into(imageView);
    }

    //游戏页面圆角的iamgeView
    public static  void setImageeCircle(Context mContext, String url, final ImageView image){
//        RequestOptions options = new RequestOptions()
//                .centerCrop()
//                .placeholder(R.mipmap.icon_placeholder)
//                .error(R.mipmap.icon_placeholder)
//                .transform(new GlideCircleImage(mContext.getApplicationContext()))
//                .diskCacheStrategy(DiskCacheStrategy.ALL);
//        Glide.with(mContext.getApplicationContext()).load(url).apply(options).into(image);
        RequestOptions myOptions = new RequestOptions().optionalTransform
                (new GlideRoundedCornersTransform(DensityUtil.dp2px( 7f)
                        , GlideRoundedCornersTransform.CornerType.ALL));
        myOptions.error(errorSoWhite);
        Glide.with(mContext)
                .load(url)
                .apply(myOptions)
                .into(image);
    }


    /**
     * 下载图片
     * @param context 上下文对象
     * @param url     下载的路径
     */
    public void downloadImage( final Context context , final String url ) {
        new Thread(() -> {
            try {
                //String url = "http://www.guolin.tech/book.png";
                FutureTarget<File> target = Glide.with(context)
                        .asFile()
                        .load(url)
                        .submit();
                final File imageFile = target.get();
                Log.d("logcat", "下载好的图片文件路径=" + imageFile.getPath());
            } catch ( Exception e ) {
                e.printStackTrace();
            }
        }).start();
    }
}